<?php
/**
 * Created by PhpStorm.
 * Author: ls
 * Time: 2024/1/10 11:11
 * Desc: 退款处理-（处理父子订单）
 */

class Listener_Order_Refunded_Order extends Listener_Base
{
    public function handle($args)
    {
        $self       = new self();
        $self->args = $args;
        $order      = $args['order'];
        // 目前只处理景点订单，后续根据需求再扩展
        if ($order['typeid'] == 5 && $order['haschild'] == 1) {
            if ($self->queue_status) {
                //推送到队列执行
                Queue::in('default', Listener_Order_Refunded_Order::class, $args);
            } else {
                $self->perform();
            }
        }
    }

    public function perform()
    {
        $order    = $this->args['order'];
        $order_id = $order['id'];

        $children = DB::select()
            ->from('member_order')
            ->where('pid', '=', $order_id)
            ->and_where('status', '=', 2)
            ->execute()
            ->as_array();
        if (!empty($children)) {

            // 处理子订单
            $data = array(
                'status' => '4',
                'close_reason' => '主订单退款同时退款子订单',
            );

            DB::update('member_order')
                ->set($data)
                ->where('pid', '=', $order_id)
                ->and_where('status', '=', 2)
                ->execute();
            $product_model = St_Product::get_product_model($order['typeid']);
            foreach ($children as $child) {
                Pay_Online_Refund::refund_start($child['ordersn'], $product_model, true, null, 0);
            }
            St_Log::write('主订单退款', array('ordersn' => $order['ordersn'], 'msg' => '主订单退款同时退款子订单'), 'event/order_canceled/log');
        }
    }
}